import axiosServer from '../request'
import type { IGoods, IResponseData } from '../../utils/types'
/**
 * 商品列表
 * @returns
 */
export const RequestProductList = ({ pageNo = 1, pageSize = 5, productKey = '', shopKey = '' }): Promise<IResponseData> => {
	return axiosServer({
		method: 'get',
		params: {
			pageNo,
			pageSize,
			productKey,
			shopKey
		},
		url: '/api/shop'
	})
}
/**
 * 商品分类
 * @returns
 */
export const RequestCategoryList = () => {
	return axiosServer({
		method: 'get',
		url: '/api/category'
	})
}
/**
 * 商品添加
 * @returns
 */
export const RequestProductAdd = (formData: FormData) => {
	return axiosServer({
		method: 'post',
		data: formData,
		//Content-Type 不能小写:content-type
		headers: { 'Content-Type': 'multipart/form-data' },
		url: '/api/shop/insert'
	})
}

/**
 * 商品删除
 * @returns
 */
export const RequestProductDeleteById = (id: number) => {
	return axiosServer({
		method: 'get',
		url: '/api/shop/delete?id=' + id
	})
}

/**
 * 商品上下架
 * id:商品id
 * putaway: 1上架 0下架
 * @returns
 */
export const RequestProductPutAway = (id: number, putaway: number) => {
	return axiosServer({
		method: 'get',
		params: {
			id,
			putaway
		},
		url: '/api/shop/putaway'
	})
}

/**
 * 根据ID查找商品
 */
export const RequestProductById = (id: number) => {
	return axiosServer({
		method: 'get',
		params: {
			id
		},
		url: '/api/shop/find'
	})
}

/**
 * 商品修改
 * @returns
 */
export const RequestProductUpdate = (product: IGoods) => {
	return axiosServer({
		method: 'post',
		data: {
			id: product.id,
			categoryId: product.categoryId,
			product: product.product,
			picture: product.picture,
			price: product.price,
			oldprice: product.oldprice,
			putaway: product.putaway,
			detail: product.detail,
			shop: product.shop
		},
		url: '/api/shop/update'
	})
}

/**
 * 商品修改-带图片
 * @returns
 */
export const RequestProductEdit = (formData: FormData) => {
	return axiosServer({
		method: 'post',
		data: formData,
		//Content-Type 不能小写:content-type
		headers: { 'Content-Type': 'multipart/form-data' },
		url: '/api/shop/edit'
	})
}

/**
 * 批量删除
 * ids:'1,2,3'
 */
export const RequestProductBatchDelete = (ids: string) => {
	return axiosServer({
		method: 'get',
		params: {
			ids
		},
		url: '/api/shop/batchdelete'
	})
}

export const goodApi = {
	RequestProductList,
	RequestCategoryList,
	RequestProductAdd,
	RequestProductDeleteById,
	RequestProductPutAway,
	RequestProductById,
	RequestProductUpdate,
	RequestProductEdit,
	RequestProductBatchDelete
}
